package com.xuanxing123.webapp.action.settings;

import com.opensymphony.xwork2.Preparable;
import com.xuanxing123.service.IndustrySegmentManager;
import com.xuanxing123.service.IndustryManager;
import com.xuanxing123.model.IndustrySegment;
import com.xuanxing123.webapp.action.BaseAction;

import java.util.List;

public class IndustrySegmentAction extends BaseAction implements Preparable {
    private IndustryManager industryManager;
    private IndustrySegmentManager industrySegmentManager;
    private List industrySegments;
    private IndustrySegment industrySegment;
    private Long id;
    private String query;

    public void setIndustrySegmentManager(IndustrySegmentManager industrySegmentManager) {
        this.industrySegmentManager = industrySegmentManager;
    }

    public List getIndustrySegments() {
        return industrySegments;
    }

    public IndustryManager getIndustryManager() {
        return industryManager;
    }

    public void setIndustryManager(IndustryManager industryManager) {
        this.industryManager = industryManager;
    }

    public List getIndustryList()
    {
        return industryManager.getAll();
    }
    
    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String industrySegmentId = getRequest().getParameter("industrySegment.id");
            if (industrySegmentId != null && !industrySegmentId.equals("")) {
                industrySegment = industrySegmentManager.get(new Long(industrySegmentId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        industrySegments = industrySegmentManager.search(query, IndustrySegment.class);
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public IndustrySegment getIndustrySegment() {
        return industrySegment;
    }

    public void setIndustrySegment(IndustrySegment industrySegment) {
        this.industrySegment = industrySegment;
    }

    public String delete() {
        industrySegmentManager.remove(industrySegment.getId());
        saveMessage(getText("industrySegment.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            industrySegment = industrySegmentManager.get(id);
        } else {
            industrySegment = new IndustrySegment();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (industrySegment.getId() == null);

        industrySegmentManager.save(industrySegment);

        String key = (isNew) ? "industrySegment.added" : "industrySegment.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}